<%= form_for(@cultural_heritage_care_and_maintenance,  :html=>{:onsubmit => "return validate(this);" }) do |f| %>
  <% if @cultural_heritage_care_and_maintenance.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@cultural_heritage_care_and_maintenance.errors.count, "error") %> prohibited this cultural_heritage_care_and_maintenance from being saved:</h2>

      <ul>
      <% @cultural_heritage_care_and_maintenance.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

<%  #si es creado por priemra vez, tons no tenía MINC creador
    #if @cultural_heritage_care_and_maintenance.obj_care_and_maintenance_responsible.blank?
      hidden_field_tag(:responsible, current_user.id)
    #else %>    
    <%#  end %>

  <%#*<div class="field">%>
    <%#*<label>Tipo de Persona:</label><br />%>
    <%#= select_tag "person_type", '<option value = "0">Natural</option><option value = "1">Jurídica</option>'.html_safe, :include_blank=>"Seleccione", :class => 'combobox' %>

  <%#*</div>%>
 <div class="field">
    <%if (user_signed_in?)%>
      <%#= f.label :Usuario%>
      <%#= f.text_field :user, :value=> current_user.id, :type=>"hidden"%>
    <%end%>
  </div>
 <fieldset id="agrupacion" >
  <legend>Datos del Cuidado y Mantenimiento</legend>
    <fieldset class="column-left" >
      <div class="field" id="cultural_heritage_care_and_maintenance_security_person">
        <%= f.label :security_person, "Encargado" %><br />
        <%= f.select :security_person, @security_people.collect { |t| [t.name + " " + t.lastname,t.id]}, {:include_blank=>"Seleccione"}, {:class => 'combobox'} %>
      </div>
      <div class="field" id="cultural_heritage_care_and_maintenance_cultural_heritage">
        <br />
        <%= f.label :cultural_heritage, "Patrimonio Cultural" %><br />
        <%= f.select :cultural_heritage, @cultural_heritages.collect { |t| [t.name,t.id]}, {:include_blank=>"Seleccione"}, {:class => 'combobox'} %>
      </div>
      <div class="field">
        <br />
        <label>Fecha Inicio</label><br />
        <%if !@cultural_heritage_care_and_maintenance.start_date.nil?%>
          <%= f.datepicker 'start_date', :value => @cultural_heritage_care_and_maintenance.start_date.strftime("%d/%m/%Y") %>
        <%else%>
          <%= f.datepicker 'start_date' %>
        <%end%>
      </div>
      <div class="field">
        <br />
        <label>Fecha Fin</label><br />
        <%if !@cultural_heritage_care_and_maintenance.end_date.nil?%>
          <%= f.datepicker 'end_date', :value => @cultural_heritage_care_and_maintenance.end_date.strftime("%d/%m/%Y")%>
        <%else%>
          <%= f.datepicker 'end_date' %>
        <%end%>
      </div>
   </fieldset>

    <fieldset class="column-right" >

    <div class="field" id="cultural_heritage_care_and_maintenance_description">
      <%= f.label :description, "Descripción" %><br />
      <%= f.text_area :description %>
    </div>

    <%#*<div class="field" id="cultural_heritage_care_and_maintenance_cultural_institution">%>
    <%#= f.label :cultural_institution, 'Institución Cultural' %>
    <%#= f.select :cultural_institution, @cultural_institutions.collect { |t| [t.name,t.id]}, {:include_blank=>"Seleccione"},{:class => 'combobox'} %>
    <%#*</div>%>

  <div class="field" id="cultural_heritage_care_and_maintenance_care_and_maintenance_type">
    <%= f.label :care_and_maintenance_type, 'Tipo' %><br />
    <%#= f.text_field :care_and_maintenance_type %><br />
    <%= f.select :care_and_maintenance_type, @cultural_care_and_maintenance_types.collect { |t| [t.name,t.id]}, {:include_blank=>"Seleccione"},{:class => 'combobox'} %>
  </div>
   </fieldset>
 </fieldset>
<%#*#si es inc o admin%>
<%if CulturalHeritage::CareAndMaintenancesController.sup_inc_roles.include?(current_user.role) %>
  <fieldset id="agrupacion" >
      <legend>Asignación Experto</legend>

      <fieldset class="column">
         <div class="field">
          <%= f.label :mayor, 'Magnitud:' %>
          <%= f.select(:mayor, options_for_select([["Mayor",1],["Menor",0]], :selected=>@lalala),{:include_blank=>"Seleccionar"}, {:onchange => "toggle(this);"}) %>
          <%#=  select_tag "cultural_heritage_care_and_maintenance_mayor",  options_for_select([["Mayor",0],["Menor",1]]) %>

          <%#= f.text_field :care_and_maintenance_type %>
          <%#= select_tag "care_and_maintenance_magnitud", '<option value = "0">Mayor</option> <option value = "1">Menor</option>'.html_safe, :include_blank=>"Seleccione", :onchange => 'toggle(this);' %>
         </div>

        <%if @cultural_heritage_care_and_maintenance.mayor == 0
            @none = "none"
          else
            @none = ""
          end
        %>

          <div id="sel_expert" class="field" style="display: <%=@none%>">
            <%= f.label :expert, 'Experto' %>
            <%= f.select :expert, @care_and_maintenance_experts.collect { |t| [t.name + " " + t.lastname,t.id]}, {:include_blank=>"Seleccione"},{:class => 'combobox'} %>
          </div>
      </fieldset>
  </fieldset>
  <%= f.text_field :supervisor, :value=> current_user.role, :type=>"hidden"%>
<%elsif CulturalHeritage::CareAndMaintenancesController.exp_inc_roles.include?(current_user.role) %>
    <%#*Tons el que registra es un miemb de inst cultural, la magnitud es mayor y el envío el id del usuario mistcult%>
    <%= f.text_field :mayor, :value=> 1, :type=>"hidden"%>
    <%= f.text_field :expert, :value=> current_user.role, :type=>"hidden"%>

<%elsif CulturalHeritage::CareAndMaintenancesController.institucion_cultural_roles.include?(current_user.role) %>
    <%= f.text_field :responsible, :value=> current_user.role, :type=>"hidden"%>
<%end%>


 <div class="actions">
    <%= f.submit "Enviar" %>
  </div>
<% end %>

<script type="text/javascript">
  //Mostrar las opciones de eliminar foto
        /*
        function toggle() {
          elems = $(".delete_photo");
          for(i=0; i < elems.length; i++)
            elems[i].style.display = (elems[i].style.display != 'none' ? 'none' : '' );
        }*/


        function toggle(oSelect) {
          var selectedValue = oSelect.options[ oSelect.selectedIndex ].value;

          var el = document.getElementById("sel_expert");

          if (selectedValue == 1){
            //alert(0);
            el.style.display = '';
          }
          else{
            //alert(1);
            el.style.display = 'none';
          }
        }
        

  // form validation function //
  function validate(form) {
    
    //input
    var encargado = form.cultural_heritage_care_and_maintenance_security_person.value;
    //select
    var cultural_heritage = form.cultural_heritage_care_and_maintenance_cultural_heritage.value;
    //input
    var startdate = form.cultural_heritage_care_and_maintenance_start_date.value;
    var enddate = form.cultural_heritage_care_and_maintenance_end_date.value;
    //textarea
    var description = form.cultural_heritage_care_and_maintenance_description.value;

    var cultural_institution = form.cultural_heritage_care_and_maintenance_cultural_institution.value;
    var type = form.cultural_heritage_care_and_maintenance_care_and_maintenance_type.value;
        
    var currentdate = new Date();

    if(encargado == "") {
      inlineMsg('cultural_heritage_care_and_maintenance_security_person','Debe seleccionar un encargado.',3);
      return false;
    };
    if(cultural_heritage == "") {
      inlineMsg('cultural_heritage_care_and_maintenance_cultural_heritage','Debe seleccionar un patrimonio cultural.',3);
      return false;
    };

    if (!checkDate(startdate)){
      inlineMsg('cultural_heritage_care_and_maintenance_start_date',errorMsg,3);
      return false;
    }
    if(_parseDate(startdate) <= currentdate) {
      inlineMsg('cultural_heritage_care_and_maintenance_start_date','La fecha de inicio debe ser posterior a la de hoy',3);
      return false;
    }
    if (!checkDate(enddate)){
      inlineMsg('cultural_heritage_care_and_maintenance_end_date',errorMsg,3);
      return false;
    }
    if(_parseDate(startdate) > _parseDate(enddate)) {
      inlineMsg('cultural_heritage_care_and_maintenance_end_date','La fecha de fin debe ser mayor a la de inicio',3);
      return false;
    }

    if(description == "") {
      inlineMsg('cultural_heritage_care_and_maintenance_description','Debe ingresar una decripción.',3);
      return false;
    };
    if(description.match(messageRegex)) {
      inlineMsg('cultural_heritage_care_and_maintenance_description','Debe ingresar una decripción válida.',3);
      return false;
    };

    if(cultural_institution == "") {
      inlineMsg('cultural_heritage_care_and_maintenance_cultural_institution','Debe seleccionar una institución cultural.',3);
      return false;
    };
    if(type == "") {
      inlineMsg('cultural_heritage_care_and_maintenance_care_and_maintenance_type','Debe seleccionar un tipo.',3);
      return false;
    };
   }

    

</script>
